package com.four.seckill.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.four.seckill.entity.Allocation;
import com.four.seckill.dao.AllocationMapper;
import com.four.seckill.service.IAllocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.four.seckill.vo.Allocation2Vo;
import com.four.seckill.vo.AllocationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-12-19
 */
@Service
public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocation> implements IAllocationService {

    @Autowired
    private AllocationMapper allocationMapper;

    @Override
    public IPage<Allocation> findAll(Integer page, Integer pageSize, AllocationVo vo) {

        Page<Allocation> allocationPage = new Page<>(page, pageSize);




        return  allocationMapper.findAll(allocationPage,vo);
    }

    @Override
    public void deleteById(Integer id) {
        allocationMapper.deleteByIds(id);
    }

    @Override
    public void addAll(Allocation2Vo vo) {
        List<String> imgList = vo.getImgList();
//        if (imgList!=null || imgList.size()<1){
//            imgList = new ArrayList<>(2);
//        }
        String s = JSON.toJSONString(imgList);
        vo.setAllRotationChart(s);
        vo.setCreatetime(new Date());

        allocationMapper.addAll(vo);
    }

    @Override
    public void updateAllocation(Allocation2Vo vo) {
        List<String> imgList = vo.getImgList();

        String s = JSON.toJSONString(imgList);

        vo.setAllRotationChart(s);
        allocationMapper.updateAllocation(vo);

    }
}
